package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealMapper setMealMapper;

    /**
     * 将菜品或套餐添加进购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart1 = new ShoppingCart();
        shoppingCart1.setUserId(userId);
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart1);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart1);
        if (shoppingCartList == null || shoppingCartList.size() == 0) {
            //说明购物车中还没加入该菜品或套餐，直接添加

            if (shoppingCartDTO.getDishId() != null) {
                //添加前需要完善插入数据的信息，如果添加的是菜品，就需要先查出菜品的信息
                //然后将菜品的信息填充到购物车实体类的属性上
//                DishVO dishVO = dishMapper.selectById(shoppingCart.getDishId());
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                List<DishFlavor> flavorList = dishMapper.selectFlavorsByDishId(shoppingCartDTO.getDishId());


                ShoppingCart sc = ShoppingCart.builder()

                        .createTime(LocalDateTime.now())
                        .amount(dish.getPrice())
                        .number(1)
                        .name(dish.getName())
                        .dishFlavor(flavorList.toString())
                        .userId(userId)
                        .image(dish.getImage())
                        .build();
                BeanUtils.copyProperties(shoppingCartDTO, sc);
                shoppingCartMapper.add(sc);
            } else {
                //说明添加进购物车的是套餐
                SetmealVO setmealVO = setMealMapper.selectById(shoppingCartDTO.getSetmealId());
                ShoppingCart sc2 = ShoppingCart.builder()
                        .createTime(LocalDateTime.now())
                        .amount(setmealVO.getPrice())
                        .number(1)
                        .name(setmealVO.getName())
                        .userId(userId)
                        .image(setmealVO.getImage())
                        .build();
                BeanUtils.copyProperties(shoppingCartDTO, sc2);
                shoppingCartMapper.add(sc2);
            }

        } else {
            ShoppingCart shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> selectUsersSelected() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList=shoppingCartMapper.selectByUserId(userId);
        return shoppingCartList;
    }

    /**
     * 购物车中的菜品或套餐减一操作
     * @param shoppingCartDTO
     */
    @Override
    public void subOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        ShoppingCart sc = shoppingCartList.get(0);
        Integer number = sc.getNumber();
        if (number>0){
            sc.setNumber(number-1);
            shoppingCartMapper.subOne(sc);
        }
        if (sc.getNumber()==0){
            shoppingCartMapper.deleteOne(sc.getId());
        }
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }


}
